package com.dotions.lucene.configuration;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

import org.apache.lucene.LucenePackage;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * Created by Dotions on 2017-10-16.
 */
@Configuration
@ConditionalOnClass(LucenePackage.class)
@EnableConfigurationProperties(LuceneProperties.class)
public class LuceneAutoConfiguration {

    private final LuceneProperties properties;

    public LuceneAutoConfiguration(LuceneProperties properties) {
        Assert.notNull(properties, "properties must not be null.");
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean(Directory.class)
    public Directory directory() {
        Assert.notNull(properties.getIndexHome(), "indexHome must not be null.");
        Path home = FileSystems.getDefault().getPath(properties.getIndexHome());
        Directory directory = null;
        try {
            directory = FSDirectory.open(home);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return directory;
    }

    @Bean
    @ConditionalOnMissingBean(IndexReader.class)
    public IndexReader indexReader(Directory directory) {
        IndexReader reader = null;
        try {
            reader = DirectoryReader.open(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reader;
    }

    @Bean
    @ConditionalOnMissingBean(IndexSearcher.class)
    public IndexSearcher indexSearcher(IndexReader indexReader) {
        return new IndexSearcher(indexReader);
    }

    @Bean
    @ConditionalOnMissingBean(Analyzer.class)
    public Analyzer analyzer() {
        // 默认使用IK分词器
        return new IKAnalyzer(true);
    }

    @Bean
    @ConditionalOnMissingBean(IndexWriterConfig.class)
    public IndexWriterConfig indexWriterConfig(Analyzer analyzer) {
        return new IndexWriterConfig(analyzer);
    }

    @Bean
    @ConditionalOnMissingBean(IndexWriter.class)
    public IndexWriter indexWriter(Directory directory, IndexWriterConfig indexWriterConfig) {
        IndexWriter indexWriter = null;
        try {
            indexWriter = new IndexWriter(directory, indexWriterConfig);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return indexWriter;
    }

}
